<?php
namespace app\houtai\controller\system;
use app\houtai\controller\BaseController;
use think\db\Expression;

class RoomController extends BaseController {
  /**
   *  @brief  : 系统管理-房间配置详情
   *  @return : page
   *  @details: 
   */
  public function indexAction() {
    $conds = [];
    $order = [];
    //////////////////////////////////////////////////////////////////////////////////////
    $search = $this->request->param('search/d', 0);
    if ($search > 0) {
      $conds = [
        ['EXP', new Expression("((a.ServerPort + 10000) LIKE '{$search}%') OR ((a.ServerID+10) LIKE '{$search}%')")],
      ];
    }
    //////////////////////////////////////////////////////////////////////////////////////
    $orderMap = [
      'B' => 'ServerName',
      'C' => 'ServerID',
      'D' => 'ServerID2',
      'E' => 'ServerPort',
      'F' => 'KindID',
      'G' => 'ServerLevel',
      'H' => 'Total',
    ];
    $this->orderField = array_key_exists($this->orderField, $orderMap) ? $this->orderField : 'H';
    $order[$orderMap[$this->orderField]] = $this->orderDirection;
    //////////////////////////////////////////////////////////////////////////////////////
    $list = table('CYBasicDB.dbo.GameRoomInfo a', 'rydb.basic')->field([
      'a.ServerID',
      new Expression("(CONVERT(nvarchar, a.ServerID +10) + 'XXXX') AS ServerID2"),  // 房间号起始
      new Expression('(a.ServerPort + 10000) AS ServerPort'),
      'a.ServerName', 'a.KindID', 'a.ServerLevel',
      '(SELECT COUNT(b.UserID) FROM CYRecordDB.dbo.GameScoreLocker b WHERE a.ServerID = b.ServerID)' => 'Total',
    ])
    ->where($conds)->order($order)->select();
    $path = \think\Request::instance()->baseUrl();
    $pager = new \org\dtcms($list, count($list), 1, count($list), false, ['path'=>$path]);
    $Total = table('CYRecordDB.dbo.GameScoreLocker', 'rydb.record')->count();
    //////////////////////////////////////////////////////////////////////////////////////
    return $this->fetch('', [
      'search'=> $search == 0 ? '' : $search,
      'list'  => $pager,
      'Total' => $Total,
    ]);
  }
}
